package org.wishwork.api.product.combine.query;

import cn.hutool.core.util.StrUtil;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.ToString;
import org.wishwork.api.ExtendBase;
import org.wishwork.exception.ExceptionEnum;
import org.wishwork.exception.JSTException;

/**
 * @description: 组合装商品
 * @author: WangYX
 * @create: 2022-04-11 15:50
 * @Version: 1.0.0
 **/
@EqualsAndHashCode
@Getter
@ToString
public class CombineProductQueryRequest extends ExtendBase {

    /**
     * 组合装商品编码，与修改时间不能同时为空，最多20
     */
    private String skuIds;

    private CombineProductQueryRequest(Integer pageIndex, Integer pageSize, String modifiedBegin, String modifiedEnd, String skuIds) {
        super(pageIndex, pageSize, modifiedBegin, modifiedEnd);
        this.skuIds = skuIds;
    }

    @Override
    public Builder toBuilder() {
        return new Builder()
                .pageIndex(this.pageIndex)
                .pageSize(this.pageSize)
                .modifiedBegin(this.modifiedBegin)
                .modifiedEnd(this.modifiedEnd)
                .skuIds(this.skuIds);
    }

    public static Builder builder() {
        return new Builder();
    }

    public static class Builder extends ExtendBase.Builder<Builder, CombineProductQueryRequest> {

        private String skuIds;

        @Override
        public Builder modifiedBegin(String modifiedBegin) {
            this.modifiedBegin = modifiedBegin;
            return this;
        }

        @Override
        public Builder modifiedEnd(String modifiedEnd) {
            this.modifiedEnd = modifiedEnd;
            return this;
        }

        @Override
        public Builder pageIndex(Integer pageIndex) {
            this.pageIndex = pageIndex;
            return this;
        }

        @Override
        public Builder pageSize(Integer pageSize) {
            this.pageSize = pageSize;
            return this;
        }

        public Builder skuIds(String skuIds) {
            if (StrUtil.isNotEmpty(skuIds)) {
                String[] split = skuIds.split(",");
                if (split.length > 20) {
                    throw new JSTException(ExceptionEnum.USER_ERROR_A0420, "组合装商品编码，与修改时间不能同时为空，最多20!");
                }
            }
            this.skuIds = skuIds;
            return this;
        }

        @Override
        public CombineProductQueryRequest build() {
            this.pageIndex = this.pageIndex == null ? 1 : this.pageIndex < 0 ? 1 : this.pageIndex;
            this.pageSize = this.pageSize == null ? 30 : this.pageSize < 0 ? 30 : this.pageSize;
            if (StrUtil.isEmpty(this.skuIds)) {
                keyIsEmptyTimeHandle(7);
            }
            modifiedTimeHandle(7);
            return new CombineProductQueryRequest(this.pageIndex, this.pageSize, this.modifiedBegin, this.modifiedEnd, this.skuIds);
        }
    }


}
